import api from 'api'
import moment from 'moment'
import store from 'store'

export default{
  namespaced:true,
  state:{
    courseDate:{
      title:'选择日期'
    },
    courseTime:{
      title:'选择时间'
    },
    list:{
      yudingStatus:2,
      guest_price:'暂无价格', //嘉宾价
      alliance_price:'暂无价格', //会员价
      pricecontain:'暂无', //价格包含
      subscribeinfo:'暂无', //预订说明
      unsubscribeinfo:'暂无', //退订说明
    },
    limitTime:{
      starttime:0,
      endtime:23
    },
    player:[],
    price:{
      serverOnePrice:20,
      alliance_price:0,
      guest_price:0,
      server_price:0,
      total_price:0
    } //金额
  },
  mutations:{
    saveCourseid(state,id){
      state.courseid = id
    },
    setData(state,res){ //设置预订时间
      state.courseDate = res.date
      state.courseTime = res.time
      this.dispatch("yuding/getYudingData")
    },
    saveTime(state,time){
      state.limitTime = {
        starttime:moment.duration(time.starttime)/3600/1000,
        endtime:moment.duration(time.endtime)/3600/1000
      }
    },
    sumPrice(state){ //计算价格
      if(state.list.yudingStatus == 1){ //预订信息有才计算
        let alliance_price = JSON.parse(JSON.stringify(state.list.alliance_price))
        let guest_price = JSON.parse(JSON.stringify(state.list.guest_price))
        let playerLength = state.player.filter(e=>e.check == 1).length
        // state.price.my_price = store.state.user.userInfo.member.isvip==1?alliance_price:guest_price
        state.price.my_price = guest_price //目前只有嘉宾
        state.price.guest_price = guest_price*(playerLength-1)
        state.price.server_price = state.price.serverOnePrice*playerLength
        state.price.total_price = parseFloat(state.price.my_price) + parseFloat(state.price.guest_price) + parseFloat(state.price.server_price)
        // console.log(state.price)
      }
    },
    reloadPlayer(state,arr){
      state.player = arr
    }
  },
  actions:{
    getYudingData(context){
      let params = {
        courseid:store.state.common.courseid,
        type:1,
        teetime:context.state.courseDate.date + Number(context.state.courseTime.time)*3600
      }
      api.get("/course/courseByDate",params).then(e=>{
        context.state.list = e.data
        context.state.list.yudingStatus = 1
        let userInfo = sessionStorage.getItem("userInfo")
        if(userInfo){
          userInfo = JSON.parse(userInfo)
          let my = {
            // always_check:1,
            check:true,
            telphone:userInfo.member.account,
            name:userInfo.member.nickname!=''?userInfo.member.nickname:'用户_'+userInfo.member.account
          }
          context.commit("reloadPlayer",[my]) //把自己添加进打球人
          context.commit("sumPrice") //计算价格
        }
      }).catch(err=>{
        context.state.list = err.data
        // context.state.list = {
        //   guest_price:'暂无价格', //嘉宾价
        //   alliance_price:'暂无价格', //会员价
        //   pricecontain:'暂无', //价格包含
        //   subscribeinfo:'暂无', //预订说明
        //   unsubscribeinfo:'暂无', //退订说明
        // }
        if(err.errcode == "40012" || err.errcode == "40015"){ //停场和无效时间都判断
          context.state.list = JSON.parse(JSON.stringify(context.state.list))
          context.state.list.yudingStatus = 0
        }
      })
    }
  }
}